//
//  LTNotes.m
//  JSONTest
//
//  Created by belle on 15/7/16.
//  Copyright (c) 2015年 belle. All rights reserved.
//


@interface LTNotes : NSObject


@end

@implementation LTNotes

+ (NSString *)printObjectOnly:(id)object
{
    if ([NSJSONSerialization isValidJSONObject:object])
    {
        
        NSError *parseError = nil;
        
        NSData *jsonData = [NSJSONSerialization dataWithJSONObject:object options:NSJSONWritingPrettyPrinted error:&parseError];
        
        return [[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding];
        
    }else
    {
        
        return object;

    }
    
   
}

+ (void)printAndSaveObject:(id)object
{
    [LTNotes saveStringToDoc:[LTNotes printObjectOnly:object]];
    
}

+ (void)saveStringToDoc:(id)str
{
    
    if ([str isKindOfClass:[str class]])
    {
        
        NSData *jsonData = [str dataUsingEncoding:NSUTF8StringEncoding];

        NSArray *paths=NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
        NSString *path=[paths objectAtIndex:0];
      
#if 0
        //文件名
        NSDate *date = [NSDate date];
        NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
        [formatter setDateFormat:@"yyyyMMhhddss"];
       NSString *fileName = [NSString stringWithFormat:@"%@.json",[formatter stringFromDate:date]];
#else
      NSString *fileName = [NSString stringWithFormat:@"notes.json"];
#endif
      
      
        NSString *Json_path=[path stringByAppendingPathComponent:fileName];
        NSLog(@"\n%@\n保存JSON到:\n\t%@",str,Json_path);
    
        [jsonData writeToFile:Json_path atomically:YES];
        
    }else
    {
        NSLog(@"没有转化成文本");
    }
     
}

@end
